package com.example.testdemo.coordinator;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.example.testdemo.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 颜 on 04.28.028.
 * 伸缩标题栏
 */

public class CoordinatorActivity extends FragmentActivity {
    private ImageView image;
    private ImageView image_blur;
    private AppBarLayout appbar;
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ImageView bottomImage;
    private SmartRefreshLayout refreshLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinator);
        image_blur = findViewById(R.id.image_blur);
        image = findViewById(R.id.image);
        appbar = findViewById(R.id.appbar);
        toolbar = findViewById(R.id.toolbar);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);
        bottomImage = findViewById(R.id.bottomImage);
        refreshLayout = findViewById(R.id.refreshLayout);
        String imgUrl = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3296169939,2964109829&fm=27&gp=0.jpg";
        Picasso.with(this).load(imgUrl).placeholder(R.mipmap.a2).into(image_blur);
        Picasso.with(this).load(imgUrl).placeholder(R.mipmap.a2).into(bottomImage);
        Picasso.with(this).load(imgUrl).transform(new BlurTransformation(this)).into(image);
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            float height;

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (height == 0) {
                    height = appBarLayout.getBottom() - toolbar.getHeight();
                }
                //将偏移量由负数转成正数
                float offset = verticalOffset < 0 ? verticalOffset * -1 : verticalOffset;
                float alpha;
                if (offset == 0) {
                    alpha = 0;
                } else {
                    alpha = offset / height;
                }
                System.out.println("透明度：" + alpha);
                image.setAlpha(alpha);
            }
        });
        tabLayout.addTab(tabLayout.newTab().setText("动态"));
        tabLayout.addTab(tabLayout.newTab().setText("喜欢"));
        tabLayout.addTab(tabLayout.newTab().setText("更多"));

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new DynamicFragment());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());

        List<String> titles = new ArrayList<>();
        titles.add("动态");
        titles.add("喜欢");
        titles.add("更多");

        // 创建ViewPager适配器
        MyAdapter myPagerAdapter = new MyAdapter(getSupportFragmentManager(), titles);
        myPagerAdapter.setFragments(fragments);

        viewPager.setAdapter(myPagerAdapter);

        tabLayout.setupWithViewPager(viewPager);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh();
                System.out.println("刷新");
            }
        });
        SmartRefreshHeader header = new SmartRefreshHeader(this,bottomImage,image,image_blur);
        refreshLayout.setRefreshHeader(header);
    }


}
